<!DOCTYPE HTML>
<html lang="en">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="CSS中的Flex弹性布局和定位, Casey Lu&#39;s Blog">
    <meta name="description" content="">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>CSS中的Flex弹性布局和定位 | Casey Lu&#39;s Blog</title>
    <link rel="icon" type="image/png" href="/medias/maozhao.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery.min.js"></script>
    
<meta name="generator" content="Hexo 4.2.0"><link rel="alternate" href="/atom.xml" title="Casey Lu's Blog" type="application/atom+xml">
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>


<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">Casey Lu's Blog</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>Index</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>Tags</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>Categories</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>Archives</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>About</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="Search" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>

<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">Casey Lu's Blog</div>
        <div class="logo-desc">
            
            Never really desperate, only the lost of the soul.
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			Index
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			Tags
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			Categories
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			Archives
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			About
		</a>
          
        </li>
        
        
    </ul>
</div>

        </div>

        
    </nav>

</header>

    
<script src="/libs/cryptojs/crypto-js.min.js"></script>
<script>
    (function() {
        let pwd = '';
        if (pwd && pwd.length > 0) {
            if (pwd !== CryptoJS.SHA256(prompt('嘻嘻嘻，访问本文章需要密码')).toString(CryptoJS.enc.Hex)) {
                alert('密码错误哦~');
                location.href = '/';
            }
        }
    })();
</script>




<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/articleimages/24.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">CSS中的Flex弹性布局和定位</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        height: calc(100vh - 250px);
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/CSS/">
                                <span class="chip bg-color">CSS</span>
                            </a>
                        
                            <a href="/tags/Flex/">
                                <span class="chip bg-color">Flex</span>
                            </a>
                        
                            <a href="/tags/Position/">
                                <span class="chip bg-color">Position</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/" class="post-category">
                                技术分享
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>Publish Date:&nbsp;&nbsp;
                    2020-03-13
                </div>
                

                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>Word Count:&nbsp;&nbsp;
                    2.1k
                </div>
                

                

                
            </div>

        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <p>在移动端的开发中，为了方便起见，Flex弹性布局成为最常见的布局方式，单纯地使用普通定位以及不能满足我们的设计需要。</p>
<h2 id="一、传统的元素定位">一、传统的元素定位</h2>
<p>传统布局上，对于元素的定位，我们主要从三方面来设置display、position 和 float，此外还有z-index</p>
<p><strong>1. display的属性：</strong></p>
<ul>
<li><strong>none</strong>： 不显示，隐藏一个元素</li>
<li><strong>inline(默认)</strong>： 默认显示内联元素，前后没有换行符</li>
<li><strong>inline-block</strong>： 行内块元素</li>
<li><strong>block</strong>：块级元素，前后有换行符</li>
<li><strong>list-item</strong>：此元素作为列表显示</li>
<li><strong>run-in</strong>：根据上下门作为块级或者内联元素显示<br>
表格单元格的显示：</li>
<li><strong>table/inline-table/table-row-group/table-header-group/table-footer-group</strong></li>
<li><strong>table-row/table-column-group/table-column/table-cell/table-caption</strong></li>
</ul>
<p><strong>※display:none 和 visibility:hidden的区别：</strong></p>
<ul>
<li><code>display:none</code>（看不见，摸不着）可以隐藏某个元素，且不会占用空间。不但被隐藏布局，占据空间也会从布局消失</li>
<li><code>visibility:hidden</code>（看不见，摸得着）可以隐藏某个元素，但隐藏的元素仍需占用空间。元素虽然被隐藏，但依旧影响布局。</li>
</ul>
<p><strong>2. position的属性：</strong><br>
- <strong>static</strong>（default）——默认定位：无定位，元素出现在正常流中(忽略top、left、bottom、right、z-index等)<br>
- <strong>absolute</strong> ——绝对定位：相对于最近的<em><strong>已定位的父元素</strong></em>，若不存在，则相对于<html>。与文档流无关，不占空间，与其他元素重叠。<br>
- <strong>relative</strong>  ——相对定位：出现在所在位置上，设置垂直、水平位置<code>(top、bottom、left、right</code>)，让这个元素相对于<em><strong>自己的起点</strong></em>移动。无论移动与否，都占据原来空间，移动元素可能导致他覆盖其他框。<br>
- <strong>fixed</strong> ——固定定位：相对于<em><strong>浏览器的窗口</strong></em>是固定位置，不随窗口滚动而移动。与文档流无关，不占据空间，与其他元素重叠。<br>
- <strong>sticky</strong> ——粘性定位：相对于该元素在流中的***flow root(BFC)和containing block(最近的块级祖先元素)***定位。按照普通文档流定位，而后元素定位表现为在跨越特定阈值之前为相对定位，之后为固定定位。<br>
- <strong>inherit</strong>：父元素处继承position属性值</p>
<p><strong>3. float属性</strong><br>
float指定一个盒子是否可以浮动：<br>
- <strong>left:</strong> son.width = som.padding + son.content<br>
- <strong>right:</strong><br>
- <strong>none:</strong> <code>空隙 = son.margin + father.padding</code><br>
- <strong>inherit:</strong><br>
clear指定不允许元素周围有浮动元素：left、right、both、none、inherit</p>
<p><strong>4. z-index：</strong> 指定一个元素的堆叠顺序，调整定位时重叠块的上下位置</p>
<p>关于float还有一个非常常见的问题——<strong>清除浮动</strong></p>
<p><strong>现象：</strong> 未给父元素设置高度时，内部元素不浮动会撑开它，但是浮动时，父元素会变成一条线<br>
<strong>本质：</strong> 为了解决父元素因为子元素浮动引起的内部高度为0的问题</p>
<p><strong>4. ※如何清除浮动？</strong><br>
<strong>一、额外标签法（不推荐）</strong><br>
在浮动元素后面添加带clear的空白标签，例如：<code>&lt;div class=&quot;clear&quot;&gt;、&lt;br/&gt;、&lt;hr/&gt;</code>之类的空元素,设置属性：</p>
<pre class=" language-language-css"><code class="language-language-css">.clear { 
  clear:both 
}
</code></pre>
<p><strong>二、父级使用overflow属性（不推荐）</strong><br>
通过触发<code>BFC</code>方式，实现清除浮动的效果，给浮动元素的父容器添加<code>overflow:hidden</code>或者<code>overflow:auto</code>可清除浮动</p>
<pre class=" language-language-css"><code class="language-language-css">.father {
  width: 400px;
  border: 1px solid deeppink;
  overflow: hidden;
}
</code></pre>
<blockquote>
<p>BFC，块级格式化上下文，用于清除浮动，防止margin重叠，BFC是一个独立的渲染区域，有一定的布局规则<br>
1）BFC区域不会和float box重叠<br>
2）BFC是页面上的一个独立容器，子元素不会影响到外面<br>
3）计算BFC的高度时，浮动元素也会参与计算</p>
</blockquote>
<p><strong>Q：哪些元素生成BFC？</strong></p>
<ul>
<li>float不为none的元素</li>
<li>position为absolute和fixed的元素</li>
<li>display为inline-block、table-cell、table-caption、flex、inline-flex的元素</li>
<li>overflow不为visible的元素</li>
</ul>
<p>此外在IE6中还需要触发<code>hasLayout</code>,例如为父容器设置宽高或者设置<code>zoom:1</code><br>
<strong>三、使用:after伪元素</strong><br>
给浮动元素的父容器添加一个<code>clearfix</code>的class，然后给这个class添加一个<code>:after</code>伪元素，实现元素末尾添加一个看不见的块元素(Block)清理浮动。:after伪元素和IEhack可以完美兼容当前的各大浏览器，这里的IEhack是指触发<code>hasLayout</code>.<br>
使用示例：<br>
父容器：<code>&lt;div class=&quot;father clearfix&quot;&gt;&lt;/div&gt;</code></p>
<pre class=" language-language-css"><code class="language-language-css">.clearfix:after{  /* 伪元素是行内元素，正常浏览器清除浮动的方法 */
  content: "";  /* content里面的内容尽量不带点 */
  display: block;
  height: 0;
  clear: both;
  visibility: hidden;
}
.clearfix{ /*IE6清除浮动的方式，*只有IE6-7执行，其他浏览器不执行*/
  *zoom: 1;
}
</code></pre>
<p>:after方式实际上是方法一中空元素的升级版，但好处是不需要添加额外的标签<br>
<strong>四、使用:before和:after双伪元素</strong><br>
类似上一个方法，给父亲添加clearfix，对父类容器的定义依旧是：<code>&lt;div class=&quot;father clearfix&quot;&gt;&lt;/div&gt;</code></p>
<pre class=" language-language-css"><code class="language-language-css">.clearfix:after,.clearfix:before {
  content: "";
  display: table;
}
.clearfix:after {
  clear: both;
}
.clearfix{
  *zoom: 1; /*原因同上*/
}
</code></pre>
<p>对于这几种清除方式的优缺点，cc也做表整理大致这样：<br>
<img src="https://img-blog.csdnimg.cn/20200313234208556.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0ODczNzEw,size_16,color_FFFFFF,t_70" alt="清除浮动的四种方式分析对比"><br>
以上都是对常见的定位类型和属性的分析，实际上，传统的布局解决方案对于特殊布局是十分不便的。比如常见的垂直居中布局并不易实现。这个时候Flex布局就可以很好地解决这个问题。</p>
<h2 id="二、Flex弹性布局">二、Flex弹性布局</h2>
<p>css中的盒模型包括IE盒子模型和标准W3C盒子模型。两者的区别在于width的包含范围，标准的盒子模型<code>width=content</code>的宽度，IE盒子模型<code>width = content+padding+border</code><br>
控制盒模型范围则需要：box-sizing属性<code>（content-box | padding-box | border-box）</code></p>
<p>盒子模型限制下的布局不易调整，<strong>Flex (Flexible Box)的出现就是为盒状模型提供最大的灵活性</strong>。</p>
<p>关于Flex布局，可以划分为：</p>
<p><strong>1. 容器的属性</strong></p>
<ul>
<li><strong>flex-direction:</strong> 决定主轴的方向(子item的排列方式)<code>flex-direction: row | row-reverse | column | column-reverse</code></li>
<li><strong>flex-wrap:</strong> 决定换行的规则,<code>flex-wrap: nowrap | wrap | wrap-reverse</code></li>
<li><strong>flex-flow:</strong> <code>flex-flow: &lt;flex-direction &gt; || &lt;flex-wrap&gt;</code></li>
<li><strong>justify-content:</strong> 对齐方式，水平主轴对齐方式</li>
<li><strong>align-items:</strong> 对齐方式，竖直轴线方向</li>
</ul>
<p><strong>2. 元素（项目）的属性</strong></p>
<ul>
<li><strong>order</strong>: 定义项目的排列顺序，顺序越小，排列越靠前，默认为0</li>
<li><strong>flex-grow</strong>：定义项目的放大比例，即使存在空间，也不会放大</li>
<li><strong>flex-shrink</strong>: 定义了项目的缩小比例，当空间不足的时候会等比例缩小，item的<code>flex-shrink=0</code>时表示不缩小</li>
<li><strong>flex-basis</strong>: 定义了在分配多余的空间，项目占据的空间</li>
<li><strong>flex</strong>: <code>[flex-grow, flex-shrink, flex-basis]</code>的简写，默认值<code>0 1 auto</code></li>
<li><strong>align-self</strong>: 允许单个项目与其他项目不一样的对齐方式，可以覆盖<code>align-items</code>,默认属性为<code>auto</code>,表示集成父元素的align-items</li>
</ul>
<h2 id="三、实现垂直居中布局">三、实现垂直居中布局</h2>
<p>垂直居中布局虽然是我们比较常见的一种布局方式，很多时候我们需要子元素在父元素内实现垂直居中，实际并不容易，接下来总结下实现垂直居中的几种方式。</p>
<p><strong>1. margin：auto法</strong></p>
<p>案例：实现图像在div内的垂直居中</p>
<pre class=" language-language-html"><code class="language-language-html"><div>
  <img src="cat.jpg">
</div>
</code></pre>
<p>css:</p>
<pre class=" language-language-css"><code class="language-language-css">div {
  width: 400px;
  height: 400px;
  position: relative;
  border: 1px solid #2e2e2e;
}
img {
  position: absolute;
  margin: auto;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
</code></pre>
<p>定位上下左右为0，margin:auto，可以实现脱离文档流的居中。</p>
<p><strong>2. margin负值法</strong></p>
<pre class=" language-language-css"><code class="language-language-css">.container{
  width: 500px;
  height: 400px;
  border: 2px solid #6e6e6e;
  position: relative;
}
.inner{
  width: 480px;
  height: 380px;
  background-color: #4699ee;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -190px; /* height的一半 */
  margin-left: -240px; /* width的一半 */
}
</code></pre>
<p>上方的margin负值也可以替换为： <code>transform: translateX(-50%)</code> 和 <code>transform: translateY(-50%)</code></p>
<p><strong>3. table-cell(未脱离文档流)</strong></p>
<p>设置父元素的<code>display:table-cell</code>，并且<code>vertical-align:middle</code>，这样子元素可以实现垂直居中。</p>
<pre class=" language-language-css"><code class="language-language-css">div{
  width: 300px;
  height: 300px;
  border: 3px solid #555999;
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}
img{
  vertical-align: middle;
}
</code></pre>
<p><strong>4. flex布局</strong></p>
<p>设置父元素为：<code>dispay:flex</code>,并且设置<code>align-items:center</code>;<code>justify-content:center;</code></p>
<pre class=" language-language-css"><code class="language-language-css">.container{
  width: 300px;
  height: 200px;
  border: 3px solid #56ff56;
  display: flex;
  -webkit-align-items: center;
  align-items: center;
  -webkit-justify-content: centent;
  justify-content: center;
}
.inner{
  border: 3px solid #656565;
  padding: 20px;
}
</code></pre>
<p><strong>5. css3属性transform</strong></p>
<pre class=" language-language-css"><code class="language-language-css">.inner{
   position: absolute
   left: 50%
   top: 50%
   transform: translate(-50%, -50%)
}
</code></pre>
<p>对比多种布局方式也发现，在开发中使用flex布局是最便捷的</p>

            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        Author:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://caseylu.gitee.io" rel="external nofollow noreferrer">Casey Lu</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        Link:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://caseylu.gitee.io/2020/03/13/14-css-zhong-de-flex-dan-xing-bu-ju-he-ding-wei/">https://caseylu.gitee.io/2020/03/13/14-css-zhong-de-flex-dan-xing-bu-ju-he-ding-wei/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        Reprint policy:
                    </i>
                </span>
                <span class="reprint-info">
                    All articles in this blog are used except for special statements
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    reprint polocy. If reproduced, please indicate source
                    <a href="https://caseylu.gitee.io" target="_blank">Casey Lu</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>Copied successfully, please follow the reprint policy of this article</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">more</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/CSS/">
                                    <span class="chip bg-color">CSS</span>
                                </a>
                            
                                <a href="/tags/Flex/">
                                    <span class="chip bg-color">Flex</span>
                                </a>
                            
                                <a href="/tags/Position/">
                                    <span class="chip bg-color">Position</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    
    <div class="social-share" data-sites="google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">谢谢大佬的小鱼干，喵~</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>
            
        </div>
    </div>

    
        <link rel="stylesheet" href="/libs/gitalk/gitalk.css">
<link rel="stylesheet" href="/css/my-gitalk.css">

<div class="card gitalk-card" data-aos="fade-up">
    <div class="comment_headling" style="font-size: 20px; font-weight: 700; position: relative; left: 20px; top: 15px; padding-bottom: 5px;">
        <i class="fas fa-comments fa-fw" aria-hidden="true"></i>
        <span>评论</span>
    </div>
    <div id="gitalk-container" class="card-content"></div>
</div>

<script src="/libs/gitalk/gitalk.min.js"></script>
<script>
    let gitalk = new Gitalk({
        clientID: 'b739596984975414aa79',
        clientSecret: 'c5792bc3376f951910111f232176fbadfa919286',
        repo: 'L0ittle.github.io',
        owner: 'L0ittle',
        admin: "L0ittle",
        id: '2020-03-13T18-17-52',
        distractionFreeMode: false  // Facebook-like distraction free mode
    });

    gitalk.render('gitalk-container');
</script>
    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;Previous</div>
            <div class="card">
                <a href="/2020/03/15/15-websocket-yuan-li-shi-xian-he-ying-yong/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/articleimages/4.jpg" class="responsive-img" alt="WebSocket的原理、实现和应用">
                        
                        <span class="card-title">WebSocket的原理、实现和应用</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            webSocket是一项可以让服务器将数据主动推送给客户端的技术，无论前后端都会接触。在实际的项目开发中，webSocket在用于双向传输和推送消息方面能做到灵活、简便、高效，发挥着不可或缺的作用。
一、什么是webSocket
webSo
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2020-03-15
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/" class="post-category">
                                    技术分享
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/HTTP/">
                        <span class="chip bg-color">HTTP</span>
                    </a>
                    
                    <a href="/tags/WebSocket/">
                        <span class="chip bg-color">WebSocket</span>
                    </a>
                    
                    <a href="/tags/Ajax/">
                        <span class="chip bg-color">Ajax</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                Next&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2020/03/12/13-vue-xiang-mu-zhong-de-shang-la-jia-zai-he-xia-la-shua-xin/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/articleimages/22.jpg" class="responsive-img" alt="Vue项目中的上拉加载和下拉刷新">
                        
                        <span class="card-title">Vue项目中的上拉加载和下拉刷新</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            在移动端的项目中，由于显示器容量的限制，经常会有许多滚动场景的需求，因此Vue项目中实现列表的上拉加载和下拉刷新是非常频繁的事情，但是在真正项目中前端对于这个的实现有非常多的坑，今天主要介绍Vue项目中插件cube-scroll、bette
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2020-03-12
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/" class="post-category">
                                    技术分享
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Vue/">
                        <span class="chip bg-color">Vue</span>
                    </a>
                    
                    <a href="/tags/Scroll/">
                        <span class="chip bg-color">Scroll</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>


<script>
    $('#articleContent').on('copy', function (e) {
        // IE8 or earlier browser is 'undefined'
        if (typeof window.getSelection === 'undefined') return;

        var selection = window.getSelection();
        // if the selection is short let's not annoy our users.
        if (('' + selection).length < Number.parseInt('100')) {
            return;
        }

        // create a div outside of the visible area and fill it with the selected text.
        var bodyElement = document.getElementsByTagName('body')[0];
        var newdiv = document.createElement('div');
        newdiv.style.position = 'absolute';
        newdiv.style.left = '-99999px';
        bodyElement.appendChild(newdiv);
        newdiv.appendChild(selection.getRangeAt(0).cloneContents());

        // we need a <pre> tag workaround.
        // otherwise the text inside "pre" loses all the line breaks!
        if (selection.getRangeAt(0).commonAncestorContainer.nodeName === 'PRE') {
            newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>";
        }

        var url = document.location.href;
        newdiv.innerHTML += '<br />'
            + 'From: Casey Lu&#39;s Blog<br />'
            + 'Author: Casey Lu<br />'
            + 'Link: <a href="' + url + '">' + url + '</a><br />'
            + '本文章著作权归作者所有，任何形式的转载都请注明出处。';

        selection.selectAllChildren(newdiv);
        window.setTimeout(function () {bodyElement.removeChild(newdiv);}, 200);
    });
</script>


<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


<!-- 代码块折行 -->

<style type="text/css">
code[class*="language-"], pre[class*="language-"] { white-space: pre !important; }
</style>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;TOC</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>



    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/libs/aplayer/APlayer.min.css">
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="4892766072"
                   fixed='true'
                   autoplay='false'
                   theme:='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/libs/aplayer/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script>

    
    <div class="container row center-align" style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            <span id="year">2020</span>
            <a href="https://caseylu.gitee.io" target="_blank">Casey Lu</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                class="white-color">63.2k</span>&nbsp;字
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/L0ittle" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:caseylu119@163.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=1351238945" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 1351238945" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;Search</span>
            <input type="search" id="searchInput" name="s" placeholder="Please enter a search keyword"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
$(function () {
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
});
</script>
    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    <!-- Global site tag (gtag.js) - Google Analytics -->


    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    

    

    
    
    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
